package com.xindaoapp.happypet.ui;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.GroupInfo;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 选择要发帖的圈子
 * 
 * @author chenghao
 * 
 */
public class SelectGroupActivity extends BaseActivity {
    private ListView vListView;

    private JoinGroupReceiver mJoinGroupReceiver;
    private SelectGroupAdapter mSelectGroupAdapter;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_select_post_group;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mJoinGroupReceiver = new JoinGroupReceiver();
        registerReceiver(mJoinGroupReceiver, new IntentFilter(Constants.JOIN_OUT_GROUP));
        vListView = (ListView) findViewById(R.id.listview);
        vListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GroupInfo info = ((GroupInfo) parent.getAdapter().getItem(position));
                Intent intent = new Intent(getBaseContext(), PostMessageActivity.class);
                intent.putExtra(Constants.KEY_GROUP_NAME, info.name);
                intent.putExtra(Constants.KEY_GROUP_MEMBER, info.membernum);
                intent.putExtra(Constants.KEY_GROUP_ICON, info.ico);
                intent.putExtra(Constants.KEY_GROUP_FID, info.fid);
                intent.putExtra(Constants.KEY_START_FROM_SELECT_GROUP, true);
                startActivityForResult(intent, Constants.REQUESTCODE_POSTED);
            }
        });
    }

    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();
        getMoccaApi().getMyGroups(new IRequest<List<GroupInfo>>() {

            @Override
            public void request(List<GroupInfo> t) {
                if (t != null) {
                    if (mSelectGroupAdapter == null) {
                        View footerView =
                                View.inflate(getBaseContext(),
                                        R.layout.list_item_footer_select_group, null);
                        footerView.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                Intent intent =
                                        new Intent(getBaseContext(), AddGroupActivity.class);
                                startActivity(intent);
                            }
                        });
                        vListView.addFooterView(footerView);
                    }
                    mSelectGroupAdapter = new SelectGroupAdapter(t);
                    vListView.setAdapter(mSelectGroupAdapter);
                    onDataArrived(true);
                } else {
                    onDataArrived(false);
                }
            }
        });
    }

    @Override
    protected int getTopBarTitleRes() {
        return R.string.select_post_group;
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        };
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mJoinGroupReceiver);
    }


    @Override
    protected int getTopBarLeftTextViewRes() {
        return R.string.cancel;
    }


    class SelectGroupAdapter extends BaseAdapter {
        private final List<GroupInfo> t;

        public SelectGroupAdapter(List<GroupInfo> t) {
            super();
            this.t = t;
        }

        @Override
        public int getCount() {
            return t.size();
        }

        @Override
        public Object getItem(int position) {
            return t.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;

            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(getBaseContext(), R.layout.list_item_select_group, null);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.member = (TextView) convertView.findViewById(R.id.group_member);
                holder.divider = convertView.findViewById(R.id.divider);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (position == getCount() - 1) {
                holder.divider.setVisibility(View.GONE);
            } else {
                holder.divider.setVisibility(View.VISIBLE);
            }

            GroupInfo info = t.get(position);

            ImageLoader.getInstance().displayImage(info.ico, holder.icon);
            holder.name.setText(info.name);
            holder.member.setText(String.format("圈成员：\t%s", info.membernum));

            return convertView;
        }

        class ViewHolder {
            ImageView icon;
            TextView name;
            TextView member;
            View divider;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REQUESTCODE_POSTED && resultCode == RESULT_OK) {
            finish();
        }
    }

    class JoinGroupReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            onLoadDatas();
        }

    }

}
